package com.fan.shop.businesslogic;

import org.apache.log4j.Logger;

import com.fan.shop.domain.PurchaseLine;
import com.fan.shop.domain.PurchaseOrder;
import com.fan.shop.domain.Stock;
import com.fan.shop.global.ShopGlobalVariables;
import com.fan.shop.process.ProcExec;
import com.fan.shop.services.PurchaseOrderService;
import com.fan.shop.services.StockService;

public class PurchaseOrderCancelProcess extends ProcExec<Integer> {

	private static Logger logger = Logger
			.getLogger(PurchaseOrderCancelProcess.class);
	private PurchaseOrderService purchaseOrderService;
	private StockService stockService;

	private String poNumber;
	private String comments;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PurchaseOrderCancelProcess.logger = logger;
	}

	public PurchaseOrderService getPurchaseOrderService() {
		return purchaseOrderService;
	}

	public void setPurchaseOrderService(
			PurchaseOrderService purchaseOrderService) {
		this.purchaseOrderService = purchaseOrderService;
	}

	public StockService getStockService() {
		return stockService;
	}

	public void setStockService(StockService stockService) {
		this.stockService = stockService;
	}

	public String getPoNumber() {
		return poNumber;
	}

	public void setPoNumber(String poNumber) {
		this.poNumber = poNumber;
	}

	public String getComments() {
		return comments;
	}

	public void setComments(String comments) {
		this.comments = comments;
	}

	@Override
	public Integer processImpl() {

		try {
			PurchaseOrder purchaseOrder = purchaseOrderService
					.getPurchaseOrder(poNumber);
			// cancel entering purchase order
			if (purchaseOrder.getStatus().equals(ShopGlobalVariables.ENTERING)) {
				for (PurchaseLine purchaseLine : purchaseOrderService
						.getPurchaseLines(poNumber)) {
					Stock stock = stockService.getStock(purchaseLine.getId()
							.getItemCode(), purchaseOrder.getStoreCode());
					int holdQty = stock.getHoldQty();
					int avaliableQty = stock.getQuantity();
					int cancelledQty = purchaseLine.getQuantity();
					if (holdQty - cancelledQty < 0) {
						setErrorMessage("Stock: "
								+ purchaseLine.getId().getItemCode() + "@"
								+ purchaseOrder.getStoreCode()
								+ " doesn't have enough holding qty!");
						return ShopGlobalVariables.FAILED;
					}

					stock.setQuantity(avaliableQty + cancelledQty);
					stock.setHoldQty(holdQty - cancelledQty);

					stockService.updateStock(stock);
				}
			}
			// cancel completed purchase order
			else if (purchaseOrder.getStatus().equals(
					ShopGlobalVariables.COMPLETED)) {
				for (PurchaseLine purchaseLine : purchaseOrderService
						.getPurchaseLines(poNumber)) {
					Stock stock = stockService.getStock(purchaseLine.getId()
							.getItemCode(), purchaseOrder.getStoreCode());
					int holdQty = stock.getHoldQty();
					int avaliableQty = stock.getQuantity();
					int cancelledQty = purchaseLine.getQuantity();
					if (holdQty - cancelledQty < 0) {
						setErrorMessage("Stock: "
								+ purchaseLine.getId().getItemCode() + "@"
								+ purchaseOrder.getStoreCode()
								+ " doesn't have enough holding qty!");
						return ShopGlobalVariables.FAILED;
					}
					stock.setQuantity(avaliableQty + cancelledQty);
					stock.setHoldQty(holdQty);

					stockService.updateStock(stock);
				}
			}
			purchaseOrder.setStatus(ShopGlobalVariables.CANCELLED);
			purchaseOrder.setComments(comments);
			purchaseOrderService.updatePurchaseOrder(purchaseOrder);
		} catch (Exception e) {
			e.printStackTrace();
			setErrorMessage(e.getMessage());
			return ShopGlobalVariables.FAILED;
		}

		return ShopGlobalVariables.SUCCESS;
	}

}
